/*
 * Copyright (C), 2002-2016, 上海亦寻信息技术有限公司
 * FileName: RetainServiceImp.java
 * Author:   linyao
 * Date:     2016-7-4 下午2:46:36
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.emm.yixun.website.service.marketing;

import java.math.BigDecimal;
//import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import com.emm.yixun.common.model.HaveReceiveRefund;
//import com.emm.yixun.common.model.MortgageRecord;
//import com.emm.yixun.common.model.NeedReceiveRefund;
import com.emm.yixun.common.model.dto.HaveReceiveRefundDto;
//import com.emm.yixun.common.model.dto.MortgageRecordDto;
//import com.emm.yixun.common.model.dto.NeedReceiveRefundDto;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.response.Response;
//import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.marketing.service.HaveReceiveRefundServiceFacade;
//import com.emm.yixun.marketing.service.MortgageRecordServiceFacade;
//import com.emm.yixun.marketing.service.NeedReceiveRefundServiceFacade;

@Service
public class HaveReceiveRefundServiceImp implements HaveReceiveRefundService {

	@Autowired
	private HaveReceiveRefundServiceFacade haveReceiveRefundServiceFacade;
	/*@Autowired
	private NeedReceiveRefundServiceFacade needReceiveRefundServiceFacade;
	
	@Autowired
	private MortgageRecordServiceFacade mortgageRecordServiceFacade;*/

	@Override
	public boolean addRefund(HaveReceiveRefundDto entity, long id, BigDecimal counterFee, BigDecimal realAmount,
			String needOrHave) {
		boolean flag = false;
		if (counterFee.compareTo(new BigDecimal("0")) >= 1) {
			// 当退款金额大于0时，增加一条保存手续费
			entity.setRealAmount(counterFee); // 手续费
			entity.setBusinessFlag("16"); // 退款手续费
			Response<HaveReceiveRefundDto> response = haveReceiveRefundServiceFacade.save(entity);
			if (!response.isSuccess()) {
				return flag;
			}
		}

		entity.setRealAmount(realAmount); // 实退金额
		if (entity.getReceivablesType().equals(BusinessTypeEnum.DEPOSIT.getNo())) {
			entity.setBusinessFlag("4"); // 退认筹
		} else if (entity.getReceivablesType().equals(BusinessTypeEnum.SUBSCRIBE.getNo())) {
			entity.setBusinessFlag("8"); // 退认购
		} else if (entity.getReceivablesType().equals(BusinessTypeEnum.SIGNED.getNo())) {
			entity.setBusinessFlag("14"); // 退签约
		} else if (entity.getReceivablesType().equals(BusinessTypeEnum.AREA.getNo())) {
			entity.setBusinessFlag("15"); // 补差款
		}
		Response<HaveReceiveRefundDto> response = haveReceiveRefundServiceFacade.save(entity);
		if (response.isSuccess()) {
			flag = true;
		}
		//20190102 退款只生成退款记录，原收款记录不变（jzliu）
		/*if (response.isSuccess()) {
			// 将原已收款/带退款信息修改为已删除
			Response<Integer> res = new Response<Integer>();
			if ("have".equals(needOrHave)) {
				//已收款退款操作
				//1.判断退款金额，如果全部退，则删掉已收记录,并设置已收款金额为0，如果部分退，则减少发生金额
				Response<HaveReceiveRefund> haveReceiveRefundResponse= haveReceiveRefundServiceFacade.findById(id);
				if(haveReceiveRefundResponse.isSuccess()){
					HaveReceiveRefund haveReceiveRefund=haveReceiveRefundResponse.getEntity();
					BigDecimal oldRealAmount=haveReceiveRefund.getRealAmount();
					if(oldRealAmount.compareTo(realAmount)>0){
						HaveReceiveRefundDto edit = new HaveReceiveRefundDto();
						edit.setId(id);
						//原已收款金额-退款金额
						edit.setRealAmount(oldRealAmount.subtract(realAmount));
						res = haveReceiveRefundServiceFacade.updateByPrimaryKey(edit, true);
					}else if(oldRealAmount.compareTo(realAmount)<=0){
						HaveReceiveRefundDto edit = new HaveReceiveRefundDto();
						edit.setId(id);
						edit.setRealAmount(BigDecimal.ZERO);
						edit.setDelFlag("1");
						res = haveReceiveRefundServiceFacade.updateByPrimaryKey(edit, true);
					}
					
					//发生退款，修改应收款状态
					Long needId=haveReceiveRefund.getReceivablesId();
    				//未收金额
    				Response<NeedReceiveRefund> nrResponse=needReceiveRefundServiceFacade.findById(needId);
    				//已收金额
    				Response<BigDecimal> hraResponse=haveReceiveRefundServiceFacade.findHaveAmount(needId);
    				if(nrResponse.isSuccess() && hraResponse.isSuccess()  && nrResponse.getEntity().getTotalReceivablesAmount().compareTo(hraResponse.getEntity())>0){
    					//更新应收款记录为未删除
    					NeedReceiveRefundDto edit = new NeedReceiveRefundDto();
						edit.setId(nrResponse.getEntity().getId());
						edit.setDelFlag("0");
						edit.setStatus("1");
						needReceiveRefundServiceFacade.updateByPrimaryKey(edit, true);
    				}
    				//更新按揭收款状态
    				if(nrResponse.isSuccess() && hraResponse.isSuccess()){
    					//查询应收款对应的按揭记录
    					MortgageRecordDto queryEntity = new MortgageRecordDto();
    					queryEntity.setNeedId(needId);
    					ResponseList<MortgageRecord> rlMrList=mortgageRecordServiceFacade.findByDto(queryEntity);
    					//当存在按揭记录才去更新
    					if(rlMrList.isSuccess() && null!=rlMrList.getEntity() && rlMrList.getEntity().size()>0){
    						List<MortgageRecord> mortgageRecords = rlMrList.getEntity();
    						//应收金额
        					BigDecimal totalReceivablesAmount=nrResponse.getEntity().getTotalReceivablesAmount();
        					//已收金额
        					BigDecimal haveAmount=hraResponse.getEntity();
        					
        					if(haveAmount.compareTo(BigDecimal.ZERO)<=0){
        						//当已收款金额小于等于0时更新按揭收款状态为未收款
        						for (MortgageRecord mortgageRecord : mortgageRecords) {
            						MortgageRecordDto updateDto = new MortgageRecordDto();
            						updateDto.setId(mortgageRecord.getId());
            						updateDto.setMortgageReceivedStatus("0");//已收款
            						mortgageRecordServiceFacade.updateByPrimaryKey(updateDto, true);
            					}
        					}else if(haveAmount.compareTo(BigDecimal.ZERO)>0 && haveAmount.compareTo(totalReceivablesAmount)<0){
        						//当已收款金额大于0而且小于应收款金额时更新按揭收款状态为部分付款
        						for (MortgageRecord mortgageRecord : mortgageRecords) {
            						MortgageRecordDto updateDto = new MortgageRecordDto();
            						updateDto.setId(mortgageRecord.getId());
            						updateDto.setMortgageReceivedStatus("1");//已收款
            						mortgageRecordServiceFacade.updateByPrimaryKey(updateDto, true);
            					}
        					}else if(haveAmount.compareTo(totalReceivablesAmount)>=0){
        						//当已收款金额大于等于应收款金额时更新按揭收款状态为已付款
        						for (MortgageRecord mortgageRecord : mortgageRecords) {
            						MortgageRecordDto updateDto = new MortgageRecordDto();
            						updateDto.setId(mortgageRecord.getId());
            						updateDto.setMortgageReceivedStatus("2");//已收款
            						mortgageRecordServiceFacade.updateByPrimaryKey(updateDto, true);
            					}
        					}
    					}
    				}
				}
			} else if ("need".equals(needOrHave)) {
				//待退款退款操作 (ym)  
				HaveReceiveRefundDto queryHaveReceiveRefundDto=new HaveReceiveRefundDto();
				queryHaveReceiveRefundDto.setReceivablesId(id);
				queryHaveReceiveRefundDto.setTransferStatus("0");
				queryHaveReceiveRefundDto.setDocType("1");
				Double havaReceiveAmount=haveReceiveRefundServiceFacade.haveReceiveAmount(queryHaveReceiveRefundDto);
				if(null==havaReceiveAmount)havaReceiveAmount=Double.valueOf("0");
				//Response<BigDecimal> hraResponse=haveReceiveRefundServiceFacade.findHaveAmount(id);
				Response<NeedReceiveRefund> resp = needReceiveRefundServiceFacade.findById(id);
				if(resp.isSuccess()){
					BigDecimal hraAmount=new BigDecimal(havaReceiveAmount) ;
					BigDecimal totalReceivablesAmount=resp.getEntity().getTotalReceivablesAmount();
					//已退金额- 待退金额，当大于等于0相当于退完了，待退金额将标示为已删除，待退列表不显示
					if(hraAmount.compareTo(totalReceivablesAmount)>=0){
						NeedReceiveRefundDto edit = new NeedReceiveRefundDto();
						edit.setId(id);
						edit.setDelFlag("1");
						res = needReceiveRefundServiceFacade.updateByPrimaryKey(edit, true);
					}
				}
				
			}
			if (res.isSuccess()) {
				flag = true;
			}
		}*/

		return flag;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.emm.yixun.website.service.marketing.HaveReceiveRefundService#getRealAmount(com.emm.yixun.common.model.dto
	 * .HaveReceiveRefundDto)
	 */
	@Override
	public Double getRealAmount(HaveReceiveRefundDto entity) {
		return haveReceiveRefundServiceFacade.haveReceiveAmount(entity);
	}
}
